package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarBuyClientVo;
import com.feicheng.lanniao.entity.CarBuyInfo;
import com.feicheng.lanniao.entity.CarCondition;
import com.feicheng.lanniao.entity.CarRentClientVo;
import com.feicheng.lanniao.entity.CarRentInfo;
import com.feicheng.lanniao.entity.CarRentalClientVo;
import com.feicheng.lanniao.entity.CarRentalInfo;
import com.feicheng.lanniao.entity.CarRentalPrice;
import com.feicheng.lanniao.entity.CarSellPrice;
import com.feicheng.lanniao.entity.Mileage;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.CarRentClientVoMapper;
import com.feicheng.lanniao.mapper.CarRentalClientVoMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.ICarConditionService;
import com.feicheng.lanniao.service.ICarRentClientVoService;
import com.feicheng.lanniao.service.ICarRentInfoService;
import com.feicheng.lanniao.service.ICarRentalClientVoService;
import com.feicheng.lanniao.service.ICarRentalInfoService;
import com.feicheng.lanniao.service.ICarRentalPriceService;
import com.feicheng.lanniao.service.ICarSellPriceService;
import com.feicheng.lanniao.service.ICarService;
import com.feicheng.lanniao.service.IMileageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-07
 */
@Service
public class CarRentalClientVoServiceImpl extends ServiceImpl<CarRentalClientVoMapper, CarRentalClientVo> implements ICarRentalClientVoService {
	@Autowired
	private ICarConditionService iCarConditionService;
	@Autowired
	private IMileageService IMileageService;
	@Autowired
	private ICarRentalPriceService iCarRentalPriceService;
	/**
	 * @see.ICarRentalClientVoService.rentCarList
	 */
	@Override
	public ResultInfo rentCarList(Integer clientId,String searchKey,Integer conditionId, Integer mileageId, Integer brandId,Integer rentalPriceId ,String type,String ownerShip,Integer currentPage,
			Integer pageSize) {
		// TODO Auto-generated method stub
		QueryWrapper<CarRentalClientVo> carQueryWapper = new QueryWrapper<CarRentalClientVo>();
		//把删除位是true加入查询条件
		carQueryWapper.eq("del_flag", true);
		//判断clientId是否为空
		if(null !=clientId && clientId!=0) {
			carQueryWapper.eq("client_id", clientId);
		}
		//判断查询条件searchKey是否为空，不为空则与数据名字关联
		if(StringUtils.hasLength(searchKey)) {
			carQueryWapper.and(i -> i.like("type", searchKey)
			.or().like("owner_ship", searchKey));

		}
		if (null !=conditionId && conditionId!=0) {
			CarCondition carCondition = iCarConditionService.getById(conditionId);
			carQueryWapper.between("car_condition",carCondition.getMinCondition(), carCondition.getMaxCondition());
		}
		if (null !=mileageId && mileageId!=0) {

			Mileage mileage = IMileageService.getById(mileageId);
			carQueryWapper.between("mileage", mileage.getMinMileage(), mileage.getMaxMilleage());
		}
		if(null !=brandId && brandId!=0) {
			carQueryWapper.eq("brand_id", brandId);
		}
		if(null !=rentalPriceId && rentalPriceId!=0) {
			CarRentalPrice carRentalPrice = iCarRentalPriceService.getById(rentalPriceId);
			carQueryWapper.between("rental_price", carRentalPrice.getMinPrice(), carRentalPrice.getMaxPrice());
			}
		//判断查询条件车辆类型是否为空或0，不为空或0则与数据名字关联
				if(StringUtils.hasLength(type)) {			
					carQueryWapper.like("type", type);
				}
		//判断查询条件车辆是否为空，不为空则与数据名字关联
		if(StringUtils.hasLength(ownerShip)) {			
			carQueryWapper.like("owner_ship", ownerShip);
		}
		IPage<CarRentalClientVo> pageInfo = new Page<CarRentalClientVo>(currentPage, pageSize);
		return ResultInfo.success(this.page(pageInfo, carQueryWapper));

	}
	/**
	 * @see.ICarRentalClientVoService.getOneRentalBuyInfo
	 */
	@Override
	public ResultInfo getOneRentalBuyInfo(Integer carId) {
		QueryWrapper<CarRentalClientVo> queryWrapper = new QueryWrapper<CarRentalClientVo>();
		// TODO Auto-generated method stub
		if( carId != null ) {
			queryWrapper.eq("car_id", carId);
			return ResultInfo.success(this.getOne(queryWrapper));
		}
		else {
			return null;
		}
	}
	@Autowired
	ICarService ICarService;
	@Autowired
	ICarRentalInfoService ICarRentalInfoService;
	/**
	 * @see.ICarRentalClientVoService.editOneCarRentalInfo
	 */
	@Override
	public ResultInfo editOneCarRentalInfo(CarRentalClientVo carRentalClientVo) {
		QueryWrapper<Car> carQuery = new QueryWrapper<Car>();
		carQuery.eq("car_id", carRentalClientVo.getCarId());
		//通过car_id 查出来的数据 将数据分别加入到三张表里
	    Car car = ICarService.getOne(carQuery);
	    car.setCarCondition(carRentalClientVo.getCarCondition());
	    car.setMileage(carRentalClientVo.getMileage());
	    car.setOwnerShip(carRentalClientVo.getOwnerShip());
	    car.setImgUrl(carRentalClientVo.getImgUrl());
	    car.setType(carRentalClientVo.getType());
	    car.setBrandId(carRentalClientVo.getBrandId());
	    QueryWrapper<CarRentalInfo> rentInfoQuery = new QueryWrapper<CarRentalInfo>();
		rentInfoQuery.eq("car_id", carRentalClientVo.getCarId());
		CarRentalInfo carInfo = ICarRentalInfoService.getOne(rentInfoQuery);
		carInfo.setRentalPrice(carRentalClientVo.getRentalPrice());
		carInfo.setTel(carRentalClientVo.getTel());
		carInfo.setWxNumber(carRentalClientVo.getWxNumber());
		ICarService.updateById(car);
		ICarRentalInfoService.updateById(carInfo);
		return ResultInfo.success();
	}
	/**
	 * @see.ICarRentalClientVoService.delOneCar
	 */
	@Override
	public ResultInfo delOneCar(Integer carRentalInfoId) {
		//判断carRentalInfoId是否为空 为空则抛异常
		// TODO Auto-generated method stub
		if( null == carRentalInfoId  || carRentalInfoId == 0 ) {
			throw new SystemException("Id为空");
		}
		CarRentalInfo car = ICarRentalInfoService.getById(carRentalInfoId);
		car.setDelFlag(false);
		ICarRentalInfoService.updateById(car);
		return ResultInfo.success();
	}
}
